package com;

import java.util.*;

public class Bash {

    FolderItem rt; //根目录文件元素
    FolderItem pos; //当前位置文件元素

    /**
     * 获取参数
     * @param param 要获取的参数
     * @param cmd   命令
     * @return 如果存在返回其在命令字符数组中的位置，如果存在返回 -1
     */
    private int getParam(String param, String[] cmd) {
        for (int i = 0; i < cmd.length; i++) {
            if (cmd[i].equals(param)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 执行命令
     * @param Order 命令
     */
    private void runOrder(String Order) {
        String[] cmd = Order.split(" ");
        switch (cmd[0]) {
            case "exit":
                System.out.println("Goodbye!");
                System.exit(0);
            case "pwd": //输出当前绝对路径
                if (pos.fatherItems == null) {
                    System.out.println("/");
                } else {
                    System.out.println(pos.getFullPath());
                }
                return;
            case "ls":
                if (!pos.ls(getParam("-r", cmd) != -1,
                        getParam("-h", cmd) != -1,
                        getParam("-d", cmd) != -1,
                        getParam("-f", cmd) != -1,
                        pos.getFullPath() + "/")) {
                    System.out.println("[empty]"); //没有任何输出结果
                }
                return;
            default:
                FolderItem target;
                boolean findRoot = false;
                if (cmd[0].equals("touch") || cmd[0].equals("mkdir")) {
                    findRoot = true;
                }
                String[] path_array = cmd[1].split("/"); //以斜杠分割路径
                Vector<String> path1 = new Vector<String>(); //数组转换成 vector
                for (String item : path_array)
                    path1.add(item);
                if (findRoot) //寻找目标文件元素的父目录
                    path1.remove(path1.lastElement());
                if (cmd[1].charAt(0) == '/') {
                    target = rt.findFolderItem(path1); //根目录
                } else {
                    target = pos.findFolderItem(path1); //相对路径
                }
                if (target == null)
                    return;

                try {
                    switch (cmd[0]) {
                        case "touch": //修改文件
                            int siz = 0;
                            boolean ishidden = false;
                            int param_pos = getParam("-size", cmd); //获取大小参数
                            if (param_pos != -1) {
                                try {
                                    siz = Integer.parseInt(cmd[param_pos + 1]);
                                } catch (Exception e) {
                                    System.out.println("param input error");
                                    return;
                                }
                            }
                            if (getParam("-h", cmd) != -1)
                                ishidden = true;
                            target.touch(path_array[path_array.length - 1], siz, ishidden);
                            return;
                        case "mkdir": //创建文件夹
                            ishidden = false;
                            if (getParam("-h", cmd) != -1)
                                ishidden = true;
                            target.mkdir(path_array[path_array.length - 1], getParam("-h", cmd) != -1);
                            return;
                        case "cd": //改变当前终端位置
                            if (!target.directory) {
                                System.out.println("not a directory");
                            } else {
                                pos = target;
                            }
                            return;
                        case "rm": //删除
                            target.rm();
                            return;
                    }

                } catch (Exception e) {
                    System.out.println("bad usage");
                    return;
                }
        }
    }

    /**
     * 进入终端
     */
    public void start() {
        System.out.println("Welcome to SCU FILE SYSTEM");
        rt = new FolderItem(0, false, true, "", null); //新建根目录
        pos = rt;
        Scanner reader = new Scanner(System.in);
        while (true) {
            String str = reader.nextLine();
            runOrder(str);
        }
    }
}
